<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="银行名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankCode">
              <j-dict-select-tag v-model="form.bankCode" placeholder="请选择银行" dictCode="bank_info" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="银行卡持有人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userName">
              <a-input v-model="form.userName" placeholder="请输入银行卡持有人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="银行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankCard">
              <a-input v-model="form.bankCard" placeholder="请输入银行卡号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="提现金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalPrice">
              <a-input-number v-model="form.originalPrice" :min="0" :precision="2" placeholder="请输入提现金额" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import Vue from "vue";
import { ORG_CODE } from "@/store/mutation-types"


export default {
  name: 'SmsShopCashLogForm',
  components: {
    JFormContainer,
    JDate,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: ()=>{},
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      form: {
        shopId:'',
        supplierId:'',
        thirdErrors:'',
        originalPrice:'',
        price:'',
        status:'',
        remark:'',
        bankName:'',
        userName:'',
        bankCard:'',
        bankCode:'',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        originalPrice:  [
          { required: true, message: '请输入提现原金额!'},
        ],
        bankCode:  [
          { required: true, message: '请输入银行卡名称!'},
        ],
        userName:  [
          { required: true, message: '请输入用户真实姓名!'},
        ],
        bankCard:  [
          { required: true, message: '请输入银行卡号!'},
        ],
      },
      url: {
        add: "/shop/smsSupplierCashLog/add",
        edit: "/shop/smsSupplierCashLog/edit",
        queryById: "/shop/smsSupplierCashLog/queryById",
        queryLastOne: "/shop/smsSupplierCashLog/queryLastOne",
      }
    }
  },
  computed: {
    formDisabled(){
      if(this.formBpm===true){
        if(this.formData.disabled===false){
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton(){
      if(this.formBpm===true){
        if(this.formData.disabled===false){
          return true
        }
      }
      return false
    }
  },
  created () {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
  },
  methods: {
    add () {
      this.edit({
        shopId:'',
        supplierId:'',
        price:'',
        status:'',
        remark:'',
        bankName:'',
        userName:'',
        bankCard:'',
        bankCode:'',
      })
    },
    edit (record) {
      this.form = Object.assign({}, record)
      this.visible = true
    },
    //渲染流程表单数据
    showFlowData(){
      if(this.formBpm === true){
        let params = {id:this.formData.dataId};
        getAction(this.url.queryById,params).then((res)=>{
          if(res.success){
            this.edit (res.result);
          }
        });
      }
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.form.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          let formData = Object.assign({}, this.form);
          formData.supplierId = Vue.ls.get(ORG_CODE);
          console.log("表单提交数据",formData)
          httpAction(httpurl,formData,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(row){
      this.form = row
    },
    shopDetail: function () {
      let that = this;
      getAction(that.url.queryLastOne, {id: Vue.ls.get(ORG_CODE)}).then((res) => {
        if (res.success) {
          that.form = res.result;
        } else {
          that.$message.warning(res.message);
        }
      });
    },
  }
}
</script>
